Improve error messages for unexpected keyword arguments in overloaded functions#20592
Draft
KevinRK29 wants to merge 10 commits intopython:masterfrom
Draft
Improve error messages for unexpected keyword arguments in overloaded functions#20592KevinRK29 wants to merge 10 commits intopython:masterfrom
KevinRK29 wants to merge 10 commits intopython:masterfrom
Conversation
This comment has been minimized.
This comment has been minimized.
JukkaL
reviewed
Jan 16, 2026
| def f(foobar: Union[int, str]) -> None: | ||
| pass | ||
|
|
||
| f(fobar=1) # E: Unexpected keyword argument "fobar" for overloaded function "f" defined on line 4; did you mean "foobar"? |
Collaborator
There was a problem hiding this comment.
Don't report the line number here. It could be useful to report it, but we generally use a note, since the function could be in a different file so line number by itself isn't sufficient.
Collaborator
Author
There was a problem hiding this comment.
That's a good point, I'll exclude the line number from the error messaging
| pass | ||
|
|
||
| f(fobar=1) # E: Unexpected keyword argument "fobar" for overloaded function "f" defined on line 4; did you mean "foobar"? | ||
| f(random=[1,2,3]) # E: Unexpected keyword argument "random" for overloaded function "f" \ |
Collaborator
There was a problem hiding this comment.
Additional test ideas:
- Test multiple invalid keyword arguments
- Test both invalid keyword argument and incompatible positional argument
- Test both valid an invalid keyword arguments in the same call
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
08e94c3 to
f3067fd
Compare
Contributor
|
Diff from mypy_primer, showing the effect of this PR on open source code: prefect (https://github.com/PrefectHQ/prefect)
+ src/prefect/futures.py:224: error: Unexpected keyword argument "_sync" for overloaded function "result" of "State" [call-overload]
+ src/prefect/utilities/engine.py:764: error: Unexpected keyword argument "_sync" for overloaded function "result" of "State" [call-overload]
+ src/prefect/task_engine.py:529: error: Unexpected keyword argument "_sync" for overloaded function "result" of "State" [call-overload]
scipy (https://github.com/scipy/scipy)
+ scipy/sparse/linalg/tests/test_interface.py:306: error: Unexpected keyword argument "axis" for overloaded function "__call__" of "_GUFunc_Nin2_Nout1" [call-overload]
+ scipy/sparse/linalg/tests/test_interface.py:307: error: Unexpected keyword argument "axis" for overloaded function "__call__" of "_GUFunc_Nin2_Nout1" [call-overload]
xarray (https://github.com/pydata/xarray)
+ xarray/tests/test_plot.py:1167: error: Unexpected keyword argument "start" for overloaded function "arange" [call-overload]
+ xarray/tests/test_plot.py:1168: error: Unexpected keyword argument "start" for overloaded function "arange" [call-overload]
|
Collaborator
|
Can you investigate the mypy_primer diff? Were there no errors reported in the past, or do we now generate multiple errors for some calls, and are the new errors valid? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR improves error messages when calling overloaded functions with unexpected keyword arguments, making it easier to identify and fix typos.